﻿Imports System.Net
Imports Microsoft.WindowsCE.Forms

Public Class frmMain
    Dim WS As WebServer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WS = WebServer.GetWebServer
        AddHandler WS.OnError, AddressOf WS_OnErrorLog
        AddHandler WS.OnAccessLog, AddressOf WS_OnAccessLog
        AddHandler WS.OnInfo, AddressOf WS_OnInfoLog
        AddHandler WS.OnWarning, AddressOf WS_OnWarningLog
        Dim VirtualRoot = MobiminLib.Util.GetWWWFolder(MobiminLib.eWWWFolder.WWWROOT)
        WS.StartWebServer(305, VirtualRoot)
        ' Sets the screen orientation to portrait.
        Button1.Enabled = False

        Dim Ips = Dns.GetHostEntry(Dns.GetHostName).AddressList

        For Each Ip In Ips
            If Not IPAddress.IsLoopback(Ip) Then
                ListView1.Items.Add(New ListViewItem(Ip.ToString))
            End If
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WS.StopWebServer()
        Application.Exit()
    End Sub

    Public Sub WS_OnAccessLog(ByVal Message As String)
        AppendFile("Access.txt", Message)
    End Sub

    Public Sub WS_OnErrorLog(ByVal Message As String)
        AppendFile("Error.txt", Message)
        MsgBox(Message)
    End Sub

    Public Sub WS_OnWarningLog(ByVal Message As String)
        AppendFile("Warning.txt", Message)
    End Sub

    Public Sub WS_OnInfoLog(ByVal Message As String)
        AppendFile("Info.txt", Message)
    End Sub

    Private Sub AppendFile(ByVal FileName As String, ByVal Msg As String)
        FileName = MobiminLib.FileManager.CombinePath(MobiminLib.Util.GetWWWFolder(MobiminLib.eWWWFolder.WWWROOT), FileName)
        Using FS As New IO.FileStream(FileName, IO.FileMode.Append, IO.FileAccess.Write)
            Using StreamW As New IO.StreamWriter(FS)
                StreamW.WriteLine(Msg)
                StreamW.Close()
            End Using
            FS.Close()
        End Using
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    End Sub
End Class
